package com.lagou.rabbitmq.demo.controller;

import com.lagou.rabbitmq.demo.OrderDlxListener;
import com.lagou.rabbitmq.demo.config.RabbitConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.Timer;
import java.util.TimerTask;

@RestController
public class PayController {

    public static String status = "";

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @RequestMapping("/pay/{order}")
    public String sendTTLMessage(@PathVariable String order) throws UnsupportedEncodingException {

        MessageProperties properties = new MessageProperties();
        properties.setExpiration("5000");
        Message message = new Message(order.getBytes("utf-8"), properties);
        rabbitTemplate.convertAndSend(RabbitConfig.ORDER_EXCHANGE, RabbitConfig.ORDER_ROUTING_KEY, message);
        status = "待支付";
        return "订单已经生成,请在5s内进行付款.";
    }

    @RequestMapping("/pay/success")
    public String paySuccess(){
        status = "支付成功";
        return "订单支付成功";
    }

    @RequestMapping("/pay/history")
    public String payHistory(){
        return OrderDlxListener.list.toString();
    }
}
















